﻿using System.Collections.ObjectModel;
using System.Text;

namespace KinonekoSoftware.Foundation.Languages
{
    public static class LanguageManager
    {
        private static readonly Dictionary<string, string>         _Global = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
        public static           ReadOnlyDictionary<string, string> Global => new ReadOnlyDictionary<string, string>(_Global);

        public static string GetText(string key)
        {
            return _Global.GetValueOrDefault(key, key);
        }


        public static string GetEnum(Enum @enum)
        {
            return GetText($"Enum.{@enum.GetType().Name}.{@enum}");
        }

        public static string GetEnum<T>(T @enum) where T : Enum
        {
            return GetText($"Enum.{@enum.GetType().Name}.{@enum}");
        }

        public static string GetViewModel(object instance)
        {
            return GetText($"App.{instance.GetType().Name}");
        }

        public static void AddProvider(LanguageProvider provider)
        {
            var unformattedLines = provider?.GetUnformattedLines(Language);
            var sb = new StringBuilder();

            //
            //
            if (unformattedLines is null)
            {
                return;
            }

            foreach (var line in unformattedLines)
            {
                if (string.IsNullOrEmpty(line))
                {
                    continue;
                }

                // 方法1 ：Formatted(sb, line, _Global);
                

                var segments = line.Split('=');

                if (string.IsNullOrEmpty(segments[0]) ||
                    string.IsNullOrEmpty(segments[1]))
                    continue;
                
                var key   = segments[0].Trim();
                var value = segments[1].Trim();

                sb.Append(value);
                sb.Replace(@"\x20", "\x20");
                sb.Replace(@"\t", "\t");
                sb.Replace(@"\n", Environment.NewLine);

                _Global.TryAdd(key, sb.ToString());
                sb.Clear();
            }
        }

        public static string CompletionButtonText
        {
            get
            {
                return Language switch
                {
                    LanguageArea.Chinese => "确认",
                    _                    => "Ok",
                };
            }
        }

        public static string CancelButtonText
        {
            get
            {
                return Language switch
                {
                    LanguageArea.Chinese => "取消",
                    _                    => "Cancel",
                };
            }
        }


        public static string DiscardButtonText
        {
            get
            {
                return Language switch
                {
                    LanguageArea.Chinese => "放弃",
                    _                    => "Discard",
                };
            }
        }

        public static string LastPageText
        {
            get
            {
                return Language switch
                {
                    LanguageArea.Chinese => "上一页",
                    _                    => "Previous",
                };
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public static LanguageArea Language { get; set; }
    }
}